package lab3;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class DigestWorker
implements Runnable
{
	private DigestInterface dInterface;
	private File file;
	
	public DigestWorker (DigestInterface di, File file)
	{
		this.dInterface = di;
		this.file = file;
	}

	@Override
	public void run()
	{
		try
		{
			FileInputStream fis = new FileInputStream (this.file);
			
			MessageDigest sha;
			sha = MessageDigest.getInstance("SHA");
	      	DigestInputStream din = new DigestInputStream(fis, sha);
	        
	      	while ((din.read()) != -1); 
	      	
	      	din.close();
	      	fis.close();
	      	
	      	byte[] digest = sha.digest();
	      	this.dInterface.printDigestOutput(digest, file.getPath());
	      	      	
		} catch (NoSuchAlgorithmException e)
		{
			// wel should not get here leaving auto generated code
			e.printStackTrace();
		} catch (IOException e)
		{
			//dido, by this time the file should already be good and stuff.
			e.printStackTrace();
		}
	}

}
